﻿//#include "../utils/winmanager.h"
//#include "../widget.h"
#include "basewidget.h"
#include <QPropertyAnimation>
//#include <QTimer>
#include <QPalette>
#include <QDebug>
#include <QToolButton>

BaseWidget::BaseWidget(QWidget *parent, bool IsShowBG) :
    QWidget(parent),btnclose(nullptr),m_IsShowExtend(IsShowBG)
{
    setAttribute(Qt::WA_DeleteOnClose);
    setAutoFillBackground(true);

    if(IsShowBG){
        QPalette palette;
        palette.setColor(QPalette::Background, QColor(0,0,0,180));
        setPalette(palette);

        btnclose = new QToolButton(this);//widget--problem.
        btnclose->setAttribute(Qt::WA_DeleteOnClose);
        btnclose->setObjectName("btnclose_window");
        btnclose->setAutoRaise(true);
        btnclose->setFixedSize(QSize(25,25));
        btnclose->setIconSize(QSize(25,25));
        btnclose->setIcon(QIcon(":/res/images/closeBtn.png"));
        btnclose->setStyleSheet(QString("QToolButton#%1{background:transparent;border:transparent;}"
                                        "QToolButton#%1:hover{margin:1px;}").
                                arg(btnclose->objectName()));

        connect(btnclose,&QToolButton::clicked,[=]{
            this->close();
        });
        btnclose->move(width() - btnclose->width(),0);
        btnclose->setVisible(IsShowBG);
        btnclose->raise();
    }
    //    resize(qobject_cast<QWidget*>(parent)->size());
}

BaseWidget::~BaseWidget()
{
    if(btnclose){
        delete btnclose;
    }
    btnclose = nullptr;
}

void BaseWidget::showWithAnimation(QWidget *widget)
{
//    widget->show();
    QPropertyAnimation *animation = new QPropertyAnimation(widget, "windowOpacity");
//    QTimer *timeAnimation = new QTimer(widget);
//    connect(timeAnimation,&QTimer::timeout,[=]{
//        timeAnimation->stop();
        widget->show();
        animation->setDuration(2000);
        animation->setStartValue(0);
        animation->setEndValue(1);
        animation->setEasingCurve(QEasingCurve::OutCubic);
        animation->start();
        connect(animation,&QPropertyAnimation::finished,[=](){
            animation->deleteLater();
//            timeAnimation->deleteLater();
        });
//    });
//    timeAnimation->start(200);
}

void BaseWidget::closeEvent(QCloseEvent *)
{
    emit onClose();
}

void BaseWidget::focusOutEvent(QFocusEvent *)
{
    emit onFocuseOut();
}

void BaseWidget::focusInEvent(QFocusEvent *)
{
    emit onFocuseIn();
}

void BaseWidget::mouseReleaseEvent(MouseEvent *)
{
    emit onClicked();
}

void BaseWidget::resizeEvent(QResizeEvent *)
{
    if(m_IsShowExtend){
        if(qobject_cast<QWidget*>(this->parent()))
            resize(qobject_cast<QWidget*>(this->parent())->size());
        if(btnclose){
            btnclose->move(width() - btnclose->width(),0);
            btnclose->raise();
        }
    }
}
